package in.muton.edushare.common;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.processing.objects.CourseDataHandler;
import in.muton.edushare.processing.objects.DeptDataHandler;
import in.muton.edushare.processing.objects.UnivDataHandler;
import in.muton.edushare.wicket.models.CayenneList;

import java.util.List;

import org.apache.cayenne.CayenneDataObject;
import org.apache.wicket.model.Model;

import com.edushare.common.dao.CourseInstancesDAO;
import com.edushare.common.dao.DepartmentsDAO;
import com.edushare.common.dao.GroupsDAO;
import com.edushare.common.dao.UniversitiesDAO;
import com.edushare.common.dao.UsersDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.GroupMembers;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class VOHelper {
	public static CayenneDataObject getFromTypeId(int type, int id){
		ORMInterface orm = EduShareSession.get().getSessionORMInterface();
		switch(type){
		case Messages.RECEIVER_TYPE_COURSE:
			return new CourseInstancesDAO(orm).getCourseInstanceById(id);
		case Messages.RECEIVER_TYPE_DEPARTMENT:
			return new DepartmentsDAO(orm).getDepartmentByPk(id);
		case Messages.RECEIVER_TYPE_UNIVERSITY:
			return new UniversitiesDAO(orm).getUnivById(id);
		case Messages.RECEIVER_TYPE_USER:
			return new UsersDAO(orm).getUserByPk(id);
		case Messages.RECEIVER_TYPE_GROUP:
			return new GroupsDAO(orm).getGroupById(id);
		}
		return null;
	}
	
	public static CayenneList<Users> getVOUsers(Object o){
		if(o instanceof Universities){
			Universities u = (Universities)o;
			return new UnivDataHandler(new Model<Universities>(u)).getEveryOne();
		}else if(o instanceof CourseInstances){
			CourseInstances c = (CourseInstances)o;
			return new CourseDataHandler(new Model<CourseInstances>(c)).getPeople();
		}else if(o instanceof Departments){
			Departments d = (Departments)o;
			return new DeptDataHandler(new Model<Departments>(d)).getPeople();
		}else if(o instanceof Groups){
			Groups g = (Groups)o;
			List<GroupMembers> l = g.getGroupMember();
			CayenneList<Users> cl = new CayenneList<Users>(null);
			for(GroupMembers ll:l){
				cl.add(ll.getUser());
			}
			return cl;
		}else if(o instanceof Users){
			CayenneList<Users> c = new CayenneList<Users>(null);
			c.add((Users)o);
			return c;
		}
		return new CayenneList<Users>(null);
	}
	
}
